package controllers;


import dto.Language;
import dto.RelatedTopicDto;
import dto.TopicDto;
import models.App;
import models.AppGroup;
import models.Topic;
import models.test.Product;
import play.i18n.Lang;
import play.mvc.Controller;
import play.mvc.With;
import services.TopicService;
import utils.ImageUtil;
import utils.PaginationInfo;

import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.logging.Logger;

@With(Secure.class)
public class Admin extends Controller{
    static Logger logger = Logger.getLogger(Admin.class.getName());

    public static void getPicture(long id) {
        Product p = Product.findById(id);
        response.setContentTypeIfNotSet("jpg");
        if (p.image!=null) {
            renderBinary(new ByteArrayInputStream(p.image));
        }
    }

    public static void inputTopic(Long topicId) {
        Topic topic = (topicId == null) ? new Topic() : (Topic) Topic.findById(topicId);
        TopicDto topicDto = TopicService.createTopicDto(topic);

        List<RelatedTopicDto> allRelatedTopics = TopicService.getAllRelatedTopicDto();
        List<App> allApps = App.all(App.class).filter("language", Lang.get()).fetch();

        Language[] allLanguages = Language.all;

        render(topicDto, allRelatedTopics, allLanguages, allApps);
    }
    
    static int PAGE_SIZE = 15;

    public static void listTopic(String page) {
        if (page==null || page.equals(""))
            page = "1";

        long total = Topic.count();
        List<Topic> topics = Topic.all().fetch(PAGE_SIZE, (Integer.parseInt(page)-1)*PAGE_SIZE);
        List<TopicDto> topicDtos = TopicService.createTopicDtos(topics);

        PaginationInfo paginationInfo = new PaginationInfo("Admin.listTopic",
                Integer.parseInt(page),
                PAGE_SIZE,
                (int)total,
                new String[]{});

        render(topicDtos, paginationInfo);
    }
    
    public static void saveTopic(TopicDto topicDto) {
        if (topicDto.topicId==null) {
            if (TopicService.isExisted(topicDto.code, topicDto.language)) {
                String err = "Da ton tai topic voi code=" + topicDto.code + ", language=" + topicDto.language;
                flash.error(err);
                renderTemplate("Admin/error.html");
            }
        }

        TopicService.mergeTopic(topicDto);
        redirect("Admin.listTopic");
    }

    public static void deleteTopic(Long id) {
//        Topic t = new Topic(id);
//        t.delete();
        TopicService.deleteTopic(id);
        redirect("Admin.listTopic");
    }

    /**
     *
     * @param topicId
     * @param type  : 1 = relatedPhoto, 2 = largePhoto
     */
    public static void getTopicImage(Long topicId, int type) {
        redirect("User.getTopicImage", topicId, type);
    }

    public static void index() {
        render();
    }
    
    public static void changeLanguage(String lang) {
        Lang.change(lang);
        redirect("Admin.index");
    }
    
    
    public static void resizeImages() {
    	//resize appGroup
    	int APP_GROUP_WIDTH = 175, APP_GROUP_HEIGHT = 130;
    	Iterable<AppGroup> iter1 = AppGroup.all(AppGroup.class).iter();
    	
    	for (AppGroup appGroup : iter1) {
    		appGroup.photo = ImageUtil.resize(appGroup.photo, APP_GROUP_WIDTH, APP_GROUP_HEIGHT);
    		appGroup.save();
		}
    	
    	//resize App
    	int APP_WIDTH = 140, APP_HEIGHT = 130;
    	Iterable<App> iter2 = App.all(App.class).iter();
    	
    	for (App one : iter2) {
    		one.photo = ImageUtil.resize(one.photo, APP_WIDTH, APP_HEIGHT);
    		one.save();
		}
    	
    	//resize Topic related
    	int TOPIC_RELATED_WIDTH = 140, TOPIC_RELATED_HEIGHT = 130;
    	Iterable<Topic> iter3 = Topic.all(Topic.class).iter();
    	
    	for (Topic one : iter3) {
    		one.relatedPhoto = ImageUtil.resize(one.relatedPhoto, TOPIC_RELATED_WIDTH, TOPIC_RELATED_HEIGHT);
    		one.save();
		}
    	
    	//
    	redirect("Admin.index");
    }
}
